﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chinaer.Dao.EF
{
    public class ExceptionDAO:Repository<Model.Entities.Model_Exception>,IDao.IExceptionDAO
    {
        public override bool Add(Model.Entities.Model_Exception entity)
        {
            return base.Add(entity);
        }
        public override bool Delete(Model.Entities.Model_Exception entity)
        {
            entity.IsDelete = true;
            return base.Update(entity);
        }
        public override bool Delete(string id)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                Model.Entities.Model_Exception model_Exception = new Model.Entities.Model_Exception();
                model_Exception = context.Exceptions.FirstOrDefault(a => a.ID == id && a.IsUse == true && a.IsDelete == false);
                if (model_Exception == null)
                {
                    Chinaer.Core.Helper.ExceptonHelper.ThrowException("Model_Address 删除编号为：" + id + "  编号不存在");
                }
                model_Exception.IsDelete = true;
                return base.Update(model_Exception);
                //return Delete(model_Address);
            }
        }
        public override int DeleteByIDs(IList<string> listID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                var queryResult = from a in context.Exceptions
                                  where listID.Contains(a.ID)
                                  select a;
                lock (queryResult)
                {
                    foreach (var item in queryResult)
                    {
                        context.Entry<Model.Entities.Model_Exception>(item).State = System.Data.Entity.EntityState.Modified;
                        item.IsDelete = true;
                    }
                }
                return context.SaveChanges();
            }
        }
        public override bool Exists(string id)
        {
            return GetByID(id) == null ? false : true;
        }
        public override Model.Entities.Model_Exception GetByID(string id)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Exceptions.FirstOrDefault(a => a.ID == id && a.IsDelete == false && a.IsUse == true);
            }
        }
        public override IList<Model.Entities.Model_Exception> Query(string sql, int pageIndex, out int recordCount, int pageSize, params System.Data.SqlClient.SqlParameter[] sqlParams)
        {
            return base.Query(sql, pageIndex, out recordCount, pageSize, sqlParams);
        }
        public override bool Update(Model.Entities.Model_Exception entity)
        {
            if (GetByID(entity.ID) == null)
            {
                Chinaer.Core.Helper.ExceptonHelper.ThrowException("Address 更新一个不存在的实体");
            }
            return base.Update(entity);
        }

    }
}
